package com.ruibang.glass.purchase.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.Constants;
import com.ruibang.glass.purchase.domain.req.PurchaseRawReq;
import com.ruibang.glass.purchase.entity.ProcuresRawMaterialInspection;
import com.ruibang.glass.purchase.entity.PurchaseArrivalOrders;
import com.ruibang.glass.purchase.entity.PurchaseArrivalProduct;
import com.ruibang.glass.purchase.entity.PurchaseSpecialApplication;
import com.ruibang.glass.purchase.mapper.ProcuresRawMaterialInspectionMapper;
import com.ruibang.glass.purchase.mapper.PurchaseArrivalOrdersMapper;
import com.ruibang.glass.purchase.mapper.PurchaseArrivalProductMapper;
import com.ruibang.glass.purchase.service.ProcuresRawMaterialInspectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 原材料验收单表，用于记录原材料的验收详情 服务实现类
 * </p>
 *
 * @author mameng
 * @since 2024-03-28
 */
@Service
public class PurchaseRawMaterialInspectionServiceImpl extends ServiceImpl<ProcuresRawMaterialInspectionMapper, ProcuresRawMaterialInspection> implements ProcuresRawMaterialInspectionService {

    @Autowired
    private PurchaseArrivalOrdersMapper purchaseArrivalOrdersMapper;

    @Autowired
    private PurchaseArrivalProductMapper purchaseArrivalProductMapper;


    @Override
    public IPage<ProcuresRawMaterialInspection> getPage(PurchaseRawReq purchaseRawReq) {
        LambdaQueryWrapper<ProcuresRawMaterialInspection> queryWrapper = Wrappers.<ProcuresRawMaterialInspection>lambdaQuery();
        if (StringUtils.isNotEmpty(purchaseRawReq.getArrivalOrderNumber())) {
            queryWrapper.like(ProcuresRawMaterialInspection::getArrivalOrderNumber, purchaseRawReq.getArrivalOrderNumber());
        }
        if (StringUtils.isNotEmpty(purchaseRawReq.getIngredientsJudgment())) {
            queryWrapper.like(ProcuresRawMaterialInspection::getIngredientsJudgment, purchaseRawReq.getIngredientsJudgment());
        }
        if (StringUtils.isNotEmpty(purchaseRawReq.getMaterialItem())) {
            queryWrapper.like(ProcuresRawMaterialInspection::getMaterialItem, purchaseRawReq.getMaterialItem());
        }
        if (StringUtils.isNotEmpty(purchaseRawReq.getInspectionNumber())) {
            queryWrapper.like(ProcuresRawMaterialInspection::getInspectionNumber, purchaseRawReq.getInspectionNumber());
        }
        if (StringUtils.isNotEmpty(purchaseRawReq.getQualityInspectionStatus())) {
            queryWrapper.eq(ProcuresRawMaterialInspection::getQualityInspectionStatus, purchaseRawReq.getQualityInspectionStatus());
        }
        if (StringUtils.isNotEmpty(purchaseRawReq.getWarehouseJudgment())) {
            queryWrapper.eq(ProcuresRawMaterialInspection::getWarehouseJudgment, purchaseRawReq.getWarehouseJudgment());
        }
        if (StringUtils.isNotEmpty(purchaseRawReq.getPhysicalChemicalJudgment())) {
            queryWrapper.eq(ProcuresRawMaterialInspection::getPhysicalChemicalJudgment, purchaseRawReq.getPhysicalChemicalJudgment());
        }
        Page<ProcuresRawMaterialInspection> materialManagementPage = baseMapper.selectPage(new Page<>(purchaseRawReq.getPageNo(), purchaseRawReq.getPageSize()),
                queryWrapper.orderByDesc(ProcuresRawMaterialInspection::getCreateTime)
        );
        return materialManagementPage;

    }

    @Override
    public void addProcuresRawMaterialInspection(ProcuresRawMaterialInspection procuresRawMaterialInspection) {
        PurchaseArrivalProduct purchaseArrivalProduct = purchaseArrivalProductMapper.selectById(procuresRawMaterialInspection.getArrivalProductId());
        purchaseArrivalProduct.setUseStatus(Constants.ONE_STR);
        purchaseArrivalProductMapper.updateById(purchaseArrivalProduct);
        //获取采购到货创建时间
        PurchaseArrivalOrders purchaseArrivalOrders = purchaseArrivalOrdersMapper.selectOne(Wrappers.<PurchaseArrivalOrders>lambdaQuery().eq(PurchaseArrivalOrders::getPurchaseArrivalOrderNumber, procuresRawMaterialInspection.getArrivalOrderNumber()).last("limit 1"));
        procuresRawMaterialInspection.setArrivalTime(purchaseArrivalOrders.getCreateTime());
        procuresRawMaterialInspection.setQualityInspectionStatus(Constants.ZERO_STR);
        baseMapper.insert(procuresRawMaterialInspection);
    }

    @Override
    public String getNumber(String number) {
        PurchaseArrivalOrders purchaseArrivalOrders = purchaseArrivalOrdersMapper.selectById(number);
        Long aLong = baseMapper.selectCount(Wrappers.<ProcuresRawMaterialInspection>lambdaQuery().eq(ProcuresRawMaterialInspection::getArrivalOrderNumberId, number));
        if (aLong == null || aLong == 0) {
            return purchaseArrivalOrders.getPurchaseArrivalOrderNumber() + "_1";
        }
        Long sumTotal = aLong + 1;
        return purchaseArrivalOrders.getPurchaseArrivalOrderNumber() + "_" + sumTotal;
    }



}
